
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class BuildGraph implements SearchAbility2{

	
	private Map<String,Set> aMap=new HashMap<String,Set>(); //�1�7�1�7�1�7�1�7�1�7�҄1�7���1�7�1�7�1�7�1�7�1�7�0�9
	private BufferedReader br=null; //�ļ���ȡ
	private BufferedWriter bw=null; //�ļ�д��

	private static String path="data/author.txt";//�1�7�1�7�0�4�1�7�0�4�1�7���1�7�1�7
	
	
	public void buildMap() throws IOException {
		File file=new File(path); 
		br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
		int count=0;
		String line="";
		Set<String> temp=new HashSet<String>();	//�1�7��author�1�7�1�7�0�8�1�7�1�7cAuthor
		String[] aList;
		
		while((line=br.readLine())!=null) {
			count++;
			/*�ָ��ַ���*/
			if(count%2==0) {
				aList=line.split("#");
				Arrays.sort(aList);
				if(aList.length>1) {
					for(int i=0;i<aList.length;i++) {
						temp=aMap.get(aList[i]);
						for(int j=0;j<aList.length;j++) {
							if(!aList[j].equals(aList[i])) {
								//System.out.println(aList[j]);
								if(aList[j]!=null||!aList[j].equals("")) {
									if(temp==null) {
									temp=new HashSet<String>();
									}
									if(!temp.contains(aList[j])) {
										temp.add(aList[j]);
									}
								} 
							}
						
						}
						aMap.remove(aList[i]); 
						aMap.put(aList[i],temp);
						temp=null;
					}
				}
			}
		}
	}
	public void writeMap() throws IOException {
		String path2="data/aMap.txt";
		File infile=new File(path2);
		for(Map.Entry<String,Set> entry:aMap.entrySet()) {
			bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(infile,true),"utf-8"));
			bw.write(entry.getKey()+"\n"+entry.getValue()+"\n");
			bw.flush();
		} 
	}
	
	public List<String>  f3() throws IOException{
		List<String> array = new ReadDoc().readAuthorSort();
		return array;
	}
	
	public String f2(String iAuthor) throws IOException{
		String str = new ReadDoc().readAMap(iAuthor);
		return str;
	}
	
	public List<String> f4() throws IOException{
		List<String> array = new ReadDoc().readKeyword();
		return array;
	}
	
	public static void  main(String[] args) throws IOException {
		//BuildGraph bGraph=new BuildGraph();
		//bGraph.buildMap();
		//bGraph.writeMap();
		
		//KeyWord keyword=new KeyWord();
		//keyword.readWord("data/yeartitle.txt");
		//keyword.writeKeyWord();
		
		//ReadDoc readdoc=new ReadDoc();
		//System.out.println(readdoc.readAuthorSort());
		//readdoc.readAuthorSort();
		//readdoc.readKeyword();
		//readdoc.readAMap("Bor-Shing Lin");
	}
	
}
